package com.swz.dcrm.ui.viewmodel;

import android.content.Context;

import androidx.lifecycle.MediatorLiveData;

import com.swz.dcrm.api.DcrmApi;
import com.swz.dcrm.model.BaseResponse;
import com.swz.dcrm.model.ClueDetail;
import com.swz.dcrm.model.ClueRecord;
import com.swz.dcrm.model.DeliveryCar;
import com.swz.dcrm.model.Page;
import com.swz.dcrm.model.TodayCount;
import com.swz.dcrm.model.VisitRecord;
import com.swz.dcrm.model.WorkRemind;
import com.swz.dcrm.util.Constant;
import com.xh.baselibrary.callback.BaseViewModel;
import com.xh.baselibrary.callback.CallBack;

import java.util.List;

import retrofit2.Retrofit;

public class TodayPlanViewModel  extends BaseViewModel {
    private Retrofit mRetrofit;
    private DcrmApi dcrmApi;
    private MediatorLiveData<BaseResponse<Page<ClueRecord>>>todayPlanNever;
    private MediatorLiveData<BaseResponse<Page<ClueRecord>>>todayPlanHas;
    private MediatorLiveData<BaseResponse<Page<ClueRecord>>>todayPlanFail;
    private MediatorLiveData<BaseResponse<TodayCount>>todayContactCount;
    private MediatorLiveData<BaseResponse<TodayCount>>todayArriveCount;
    private MediatorLiveData<BaseResponse<TodayCount>>todayBookingCount;
    private MediatorLiveData<BaseResponse<ClueDetail>> clueDetail;
    private MediatorLiveData<BaseResponse<List<String>>>nearlyDeliveryCount;
    private MediatorLiveData<BaseResponse<Page<DeliveryCar>>> nearlyDelivery;
    private MediatorLiveData<BaseResponse<List<WorkRemind>>> workRemind;


    public TodayPlanViewModel(Retrofit retrofit) {
        this.mRetrofit = retrofit;
        dcrmApi= mRetrofit.create(DcrmApi.class);
    }

    public  MediatorLiveData<BaseResponse<TodayCount>> getTodayCount(String type){
        switch (type){
            case Constant.TODAY_ARRIVE:
                todayArriveCount=creatLiveData(todayArriveCount);
                if(todayArriveCount.getValue()!=null){
                    todayArriveCount.setValue(null);
                }
                dcrmApi.findTodayCount(Constant.URL_todayComeShopCount)
                        .enqueue(new CallBack<>(this,todayArriveCount));
                return todayArriveCount;
            case Constant.TODAY_BOOKING:
                todayBookingCount=creatLiveData(todayBookingCount);
                if(todayBookingCount.getValue()!=null){
                    todayBookingCount.setValue(null);
                }
                dcrmApi.findTodayCount(Constant.URL_todayOrderCount)
                        .enqueue(new CallBack<>(this,todayBookingCount));
                return todayBookingCount;
            case Constant.TODAY_CONTACT:
                todayContactCount=creatLiveData(todayContactCount);
                if(todayContactCount.getValue()!=null){
                    todayContactCount.setValue(null);
                }
                dcrmApi.findTodayCount(Constant.URL_todayContactCount)
                        .enqueue(new CallBack<>(this,todayContactCount));
                return todayContactCount;
        }
        return null;
    }

    public MediatorLiveData<BaseResponse<Page<ClueRecord>>> getTodayContact(String type,int status,int page,int size){
        String url=null;
        switch (type){
            case Constant.TODAY_ARRIVE:
                url=Constant.URL_todayComeShop;
                break;
            case Constant.TODAY_BOOKING:
                url=Constant.URL_todayOrder;
                break;
            case Constant.TODAY_CONTACT:
                url=Constant.URL_todayContact;
                break;
        }
        switch (status){
            case 1:
                todayPlanNever=creatLiveData(todayPlanNever);
                if(todayPlanNever.getValue()!=null){
                    todayPlanNever.setValue(null);
                }
                dcrmApi.findTodayContact(url,status,page,size)
                        .enqueue(new CallBack<>(this,todayPlanNever));
                return todayPlanNever;
            case 2:
                todayPlanHas=creatLiveData(todayPlanHas);
                if(todayPlanHas.getValue()!=null){
                    todayPlanHas.setValue(null);
                }
                dcrmApi.findTodayContact(url,status,page,size)
                        .enqueue(new CallBack<>(this,todayPlanHas));
                return todayPlanHas;
            case 3:
                todayPlanFail=creatLiveData(todayPlanFail);
                if(todayPlanFail.getValue()!=null){
                    todayPlanFail.setValue(null);
                }
                dcrmApi.findTodayContact(url,status,page,size)
                        .enqueue(new CallBack<>(this,todayPlanFail));
                return todayPlanFail;

        }
       return null;
    }
    public MediatorLiveData<BaseResponse<ClueDetail>> getClueDetail(long clueId){
        clueDetail=creatLiveData(clueDetail);
        if(clueDetail.getValue()!=null&&clueDetail.getValue().getData().getBsClueVO().getId()!=clueId){
            clueDetail.setValue(null);
        }
        dcrmApi.findClueDetailByClueId(clueId)
                .enqueue(new CallBack<>(this,clueDetail));
        return clueDetail;
    }
    public MediatorLiveData<BaseResponse<List<String>>> getNearlyDeliveryCount(){
        nearlyDeliveryCount=creatLiveData(nearlyDeliveryCount);
        dcrmApi.findNearlyDeliveryCount().enqueue(new CallBack<>(this,nearlyDeliveryCount));
        return nearlyDeliveryCount;
    }

    public MediatorLiveData<BaseResponse<Page<DeliveryCar>>>getNearlyDelivery(int day, int page, int size){
        nearlyDelivery=creatLiveData(nearlyDelivery);
        dcrmApi.findNearlyDelivery(day,page,size).enqueue(new CallBack<>(this,nearlyDelivery));
        return nearlyDelivery;
    }

    public MediatorLiveData<BaseResponse<List<WorkRemind>>> getWorkRemind() {
        workRemind=creatLiveData(workRemind);
        dcrmApi.getWorkRemind().enqueue(new CallBack<>(this,workRemind));
        return workRemind;
    }
}

